<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <el-button @click="dialogFormVisible=true">添加模块</el-button>
        <!-- <el-button type="success" plain>添加菜单</el-button> -->
        <!-- <el-button type="danger" plain>批量删除</el-button> -->
      </el-col>
    </el-row>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%" class="no-r-b"
      :header-cell-style="{background:'#FAFAFA',color:'#666666'}">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="创建日期">
        <template slot-scope="scope">
          <span>{{ scope.row.created_at | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="模块控制器">
        <template slot-scope="scope">
          <span>{{ scope.row.modspace }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="模块名称">
        <template slot-scope="scope">
          <span>{{ scope.row.modname }}</span>
        </template>
      </el-table-column>

      <el-table-column class-name="status-col" label="状态">
        <template slot-scope="{row}">
          <el-tag :type="row.deleted_at | statusFilter">
            {{ row.deleted_at?'已禁用':'正常' }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" icon="el-icon-edit" @click="editShow(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getModList" />

    <!--列表数据添加/编辑开始  -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" center width="95%" class="user">
      <el-form ref="ruleForm" :model="moduleRow" label-width="85px">
        <el-form-item label="模块名称">
          <el-input v-model="moduleRow.modname" auto-complete="off" />
        </el-form-item>
        <el-form-item label="模块控制器">
          <el-input v-model="moduleRow.modspace" auto-complete="off" />
        </el-form-item>
        <!-- <el-form-item> -->
          <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
          <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
        <!-- </el-form-item> -->
      </el-form>
    </el-dialog>
    <!--列表数据添加/编辑结束  -->
  </div>
</template>

<script>
import { getmodule, addModule, editModule } from '@/api/user'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import { Message } from 'element-ui'

export default {
  name: 'ModuleList',
  components: { Pagination },
  filters: {
    statusFilter(status) {
      if (status) {
        status = 'deleted'
      } else {
        status = 'published'
      }
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      // 弹窗标题
      dialogTitle: '模块添加',
      dialogFormVisible: false,
      moduleRow: {
        modspace: '',
        modname: ''
      }// 表单数据
    }
  },
  created() {
    this.getModList()
  },
  methods: {
    // 获取列表数据
    getModList() {
      this.listLoading = true
      getmodule(this.listQuery).then(response => {
        const { data } = response
        this.list = data.list
        this.total = data.total
        this.listLoading = false
      })
    },
    // 表单提交方法
    submitForm(formName) {
      const isadd = this.dialogTitle !== '模块编辑'
      if (isadd) { // 新增数据
        addModule(this.moduleRow).then(res => {
          Message({
            message: '保存成功',
            type: 'success',
            duration: 2 * 1000
          })
          this.dialogFormVisible = false
          this.getModList()
        })
      } else { // 编辑数据
        editModule(this.moduleRow).then(res => {
          Message({
            message: '保存成功',
            type: 'success',
            duration: 2 * 1000
          })
          this.dialogFormVisible = false
          this.getModList()
        })
      }
    },
    // 搜索日期变化操作方法
    dateChange(options) {
      console.log(options, 'dateChange')
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 编辑
    editShow(row) {
      this.moduleRow = row
      this.dialogTitle = '模块编辑'
      this.dialogFormVisible = true
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
